package org.pr.ddddesigner.infrastructure.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import reactor.core.CorePublisher;
import reactor.core.publisher.Mono;

/**
 * @description: 接口参数序列化
 * @projectName:ddd-designer
 * @see:org.pr.ddddesigner.infrastructure.config
 * @author:LXC
 * @createTime:2024/4/20 00:38
 * @version:1.0
 */
@Configuration
public class JsonConfig {

    @Bean
    public ObjectMapper getJacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        SimpleModule simpleModule = new SimpleModule();
        SimpleAbstractTypeResolver simpleAbstractTypeResolver = new SimpleAbstractTypeResolver();
        //  添加接口映射子类
        simpleAbstractTypeResolver.addMapping(CorePublisher.class, Mono.class);
        simpleModule.setAbstractTypes(simpleAbstractTypeResolver);
        objectMapper.registerModule(simpleModule);

        return objectMapper;
    }
}
